package b3.widget.home.operation
{
	import b3.config.*;
	import b3.model.*;
	import b3.net.*;
	import b3.scene.SceneManager;
	import b3.scene.build.BuildScene;
	import b3.scene.home.HomeScene;
	import b3.scene.home.grid.BuildingGrid;
	import b3.widget.basic.*;
	
	import flash.events.MouseEvent;
	
	public class OperationAssist extends BasicIcon
	{
		private var buildingModel:BuildingModel;
		
		public function OperationAssist(buildingModel:BuildingModel)
		{
			this.buildingModel = buildingModel;
			var IconBgr:Class = ResourceHelper.getInstance().getClassByName("OperationAssist");
			super(IconBgr, "协助", 1, true);
			disableMouseOverTip();
			addEventListener(MouseEvent.CLICK, onMouseClick);
			this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			HomeScene.getInstance().centerInfoPane.showOperationTip("OperationAssist", "协助建设获得光明币", 0x00AAFF);
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			HomeScene.getInstance().centerInfoPane.clear();
		}
		
		private function onMouseClick(event:MouseEvent):void
		{
			if (HomeScene.getInstance().getOperationMode() == HomeScene.NONEMODE)
			{
				var me:Player = PlayerManager.getInstance().me;
				me.addEventListener( ModelEvent.CHECK_AND_BUILD_COMPLETE, onCheckAndBuildComplete);
				me.checkAndBuild();
			}
		}
		
		private function onCheckAndBuildComplete(event:ModelEvent):void
		{
			var me:Player = PlayerManager.getInstance().me;
			me.removeEventListener(ModelEvent.CHECK_AND_BUILD_COMPLETE, onCheckAndBuildComplete);
			var data:Object = event.data;
			if (data['err'] == 0)
			{
				var buildingIndex:int = buildingModel.buildingIndex;
				var oldState:StateModel = new StateModel(me.userId);
				oldState.handleLoadStates(data['oldState']);
			 	SceneManager.getInstance().loadScene(BuildScene.getInstance());
			 	BuildScene.getInstance().changeParamByState(oldState);
			 	BuildScene.getInstance().startRebuild(buildingModel, me.maxHeight(buildingIndex), me.maxRoofType(buildingIndex));
			}
		}
	}
}